查看原文
其他

《风格的要素》中的编程格言

董强 Thoughtworks洞见 2022-03-15

写在前面:

之所以翻译这篇文章,因为它与我之前关于如何/为什么要写出更易懂的代码的思考有很多关联,但更进一步的是,这篇文章直接指出了写作与编程在原则上的相似性,本质上他们都是一种面向人类的表达。
其次,本文的英文原文博客虽然已经关闭了,但在英文语境下的人们依然对这篇“著名”文章议论纷纷(Reddit, Medium, Ycombinator),一部分原因是本文中提到的 The Elements of Style / 《风格的要素》这本书在英语世界里举足轻重,加之绝大多数编程语言都是以英语为基础,人们对于这种表达的相似性理解起来会更加自然;相对在中文语境下,鲜有人从英文写作这样的角度出发解释这种清晰表达是一种基础能力的重要性。
最后,《风格的要素》也是非常值得一读的书,对如何写出更好的文章有所帮助。 

The Programming Aphorisms of Strunk & White

《风格的要素》中的编程格言

It's been the bane of English students and the bible of creative writing teachers since the second World War. It's older than you are, meaner than you are, and presumably, built from sterner stuff than you are. This book has survived air raids and artillery shellings. Its simple lessons of thrift and economy have denuded the world of more bad writing, over the last 70 years, than the shutdown of Google Reader and Geocities combined. That is why The Elements of Style, by William Strunk and E.B. White, is a classic, and that's why you'll find a copy of this book (no thicker than the width of your finger) ensconced firmly in the "software" section of our bookshelf.

自二战以来,它就是英文学生的祸根和创造性写作教师的圣经。它比你更老,比你更刻薄,而且很可能比你更坚韧。这本书从空袭和炮火连天中生存下来。在这七十多年中,它关于精简节约的训导清理了这世界上太多的烂文章,甚至比关闭 Google Reader 和 Geocities 加起来还要多。这就是为什么这本由 William Strunk 和 E.B. White 所著的《风格的要素》如此经典,甚至你会发现,这就是为什么这本比你手指还要细的书的一份副本竟被我们摆放在书架关于“软件”的区域。

Of course, “Strunk and White”, as the book is usually called, has nothing to do with software (it was originally written in 1920) and everything to do with writing: grammar, composition, and style for users of the English language. But in its 100 pages this book has more to say about thecraftof software than many books you’ll find in the “Software” section of your local bookstore.

当然,这本经常被称为 “Strunk and White” 的书,确实跟软件没什么关系(它最早写于1920年),它所有内容都是关于写作的:语法、行文、和英文使用者的风格等。但在这100页中,这本书其实讲了很多关于软件技艺的道理,比你从你那的书店里“软件”区域中找到的许多书还要多。

2.12. Choose asuitable design and hold to it.

A basic structural design underlies every kind of writing programming. Writers Programmers will in part follow this design, in part deviate from it, according to their skills, their needs, and the unexpected events that accompany the act of composition. writing Programming, to be effective, must follow closely the thoughts of the writer programmer, but not necessarily in the order in which those thoughts occur. This calls for a scheme of procedure….in most cases, planning must be a deliberate prelude to writing programming. The first principle of composition software development, therefore, is to foresee or determine the shape of what is to come and pursue that shape

2.12 选一个适当的设计并保持下去

每一种 写作 程序都有一个基本的结构设计。 作家们 程序员们一方面力图遵循这种结构设计,一方面又会偏离它,这完全取决于他们的技巧、需要以及创作过程中出现的各种意外事件。 写作 程序要写得好,必须紧紧地顺着 作者 程序员的思路,但并不一定完全按照这些思路出现的先后顺序。这就需要一个规划……在大多数情况下, 创作 编程之前必须要有一个认真考虑过的计划。因此, 写作 软件开发的第一原则就是要预先考虑或确定将要写成的模样,并尽力为之。

A casual search-and-replace for “writing” with “programming” consistently yields paragraphs that might have been plagiarized, whole cloth, from Code Complete. Paragraphs like this one, which cautions the would-be writer programmer to design appropriately for the scale and complexity of the thing being built.

只消把“writing“全都替换为”programming“,这些段落就变成好像是从《代码大全》中抄袭改编的一样。像这样的段落,它提醒那些想成为作家的程序员适当地设计正在构建的东西的规模和复杂性。

5.3. Work from a suitable design.

Before beginning to compose develop something, gauge the nature and extent of the enterprise and work from a suitable design. Design informs even the simplest structure, whether of brick and steel or of prose. You raise a pup tent from one sort of vision, a cathedral from another. This does not mean that you must sit with a blueprint always in front of you, merely that you had best anticipate what you are getting into. To compose a laundry list, you can work directly from the pile of soiled garments, ticking them off one by one. But to write a biography, you will need at least a rough scheme; you cannot plunge in blindly and start ticking off after the fact about your subject, less you miss the forest for the trees and there be no end to your labors.

5.3. 从一个适当的设计着手

在开始 创作 开发某些东西之前,先衡量整件事的性质和范围并从一个适当的设计着手。设计是赋予哪怕最简单的结构以意义的,不论砖或钢或散文。你可以由某种想象搭建起小帐篷,也可以由另一种愿景建造一个大教堂。但这并不意味着你必须时刻坐在图纸前,它只是意味着你有了一个对于你着手要做的事情的最好预测。要撰写一个洗衣清单,你直接从一堆脏衣服开始,一件一件地勾掉即可。但要撰写一篇人物传记,你不能盲目莽撞地一开始也把要写的人物事迹一件一件地勾掉;你至少得有一个粗略的计划,以免见树不见林,最终劳而无功。

Or this, about the dangers of pet languages, non-standard libraries, young people, and drums. Chase it with a shot of Sailing to Byzantium for good measure.

还有这个,关于不成熟的语言、非标准库、年轻人和鼓噪声的危险。追求这些东西配点烈酒效果更佳。[译者注:Sailing to Byzantium可能指一种爱尔兰威士忌,作者这里的意思是不要被这些东西冲昏了头脑]

5.21. Prefer the standard to the offbeat

Young writers Inexperienced programmers will be drawn at every turn toward eccentricities in language. They will hear the beat of new vocabulariesabstractions, the exciting rhythms of special segments of their society industry, each speaking a language of its own. All of us come under the spell of these unsettling drums; the problem for beginners is to listen to them, learn the words, feel the vibrations, and not be carried away.

5.21. 宁可标准不要另类

年轻的作家们 没经验的程序员们每一次都会被语言中的怪癖所吸引。他们听说了新奇的 词汇 抽象,像鼓点一样, 社会行业中特定领域令人亢奋的节奏,每个人各说各话。我们所有人都忍受这些令人不安的鼓声的咒语。对于初学者来说,问题是要倾听、吸收那些词语,感受其脉搏,不要被带跑。

Sometimes the software interpretation rings even truer than the original meaning of the phrase:

有时候这种软件解释甚至比文本的原意更真实:

2.17. Omit needless words code

Vigorous writing programming is concise. A paragraphfunction should contain no unnecessary sentencesstatements, a sentence statement no unnecessary wordsexpressions, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. This requires not that the writer programmer make all paragraphs functions short, or avoid all detail and treat subjects only in outline, but that every word line of code tell.

2.17. 去除不必要的 词汇 代码

生动有力的 写作 程序需要简洁明了。一幅画上不应该有多余的线条,一台机器不应该有多余的零件。同样的道理,一个 段落 函数不应含有不必要的 句子 语句,一个 句子 语句不应含有不必要的 单词 表达式。这并不是要求 写作者 程序员要把所有的 段落 函数写得很短,或者避免所有的细节只把主题当作主线;而是说,每 个字 行代码都表达了本意。

2.19. Write Express coordinate ideas in similar form

This principle, that of parallel construction, requires that expressions similar in content and function be outwardly similar. The likeness of form enables the reader to recognize more readily the likeness of content and function.

2.19. 用相似的形式 写出 表达对等的意思

平行结构这个原则要求内容及功能上相似的表述要表现地相似。这种形式上的相似性使读者能够更容易识别出功能和内容本身的相似性。

Sometimes the attempt to replace writing with programming yields new insights.

有时候把写作替换成编程还会产生新的见解。

5.9. Do not affect a breezy manner

The volume of writing source code is enormous, these days, and much of it has a sort of windiness about it, almost as though the author programmer were in a state of euphoria. “Spontaneous me,” sang Whitman, and, in his innocence, let loose the hordes of uninspired scribblers script kiddies who would one day confuse spontaneity with genius.

5.9. 不要肆意妄为

如今的 文章 源代码篇幅都很长,而且其中不少是夸夸其谈的,仿佛 作者 编程的人得了欣快症似的。Whitman赞颂“不由自主的自我”,却无意地怂恿了一批 平凡拙劣的作家 毫无创意的脚本小子,总会把自然的冲动当成天赋的才华。

What does it mean to have a “breezy” style as a programmer? A plausible definition might be coding things in the order they occur to you, or to the degree they interest you. It’s a great recipe for spaghetti or, for the true code gourmand, mud. But without being “breezy” it’s also important to:

一个程序员“肆意妄为”是什么意思?一个可能的定义是,想到什么就写什么,或者取决于对它们感兴趣的程度。这其实就是产生意大利面条式代码的原因[译者注:意大利面条指一种很难维护的代码风格],而对于真正的代码美食家来说,那就是一滩浆糊[译者注:很容易让人联想到大泥球]。同时,尽管不能肆意妄为,仍需注意的是:本意。

5.2. Write Program in a way that comes naturally

Write Program in a way that comes easily and naturally to you, using words and phrases APIs and statements that come readily to hand. But do not assume that because you have acted naturally your product is without flaw.

5.2. 文章 程序要写得自然

文章 程序要写得自然流畅,要使用顺手易用的 单词和句子APIs和语句。但不要以为只要顺畅自然就可以写出毫无瑕疵的作品。

The act of writing code is a simultaneous conversation with two different audiences: the compiler/interpreter, and the other programmers on your team, including the future version of yourself. Good conversationalists—good coders—know how to speak to both audiences naturally and without affect. They know how to achieve clarity…

编写代码这件事其实是在同时与两个截然不同的听众对话:一个是编译器/解释器,另一个是你团队中其他的程序员,包括未来的你自己。会聊天的人——优秀的编码者——懂得如何同时与这两个听众顺畅自然地对话而不被影响。他们懂得如何做到清楚明了…

5.16. Be clear.

Clarity is not the prize in writing programming, nor is it always the principle mark of good style. There are occasions when obscurity serves a literary programmer yearning, if not a purpose, and there are writers programmers whose mien is more overcast than clear. But since writing programming is communication, clarity can only be a virtue.

5.16. 要写得清楚明了

清楚明了并不是 写作 编程的目标,也不总是良好风格的原则。有时候晦涩朦胧可以满足程序员自己的某种情结,即使那并不是最终目的,并且有的 作家 程序员的风格更是风云变幻而非清晰明朗。不过既然 写作 编程是一种交流,那么清楚明了便是美德。

…without being verbose:

……不要太啰嗦:

5.11. Do not explain too much.

It is seldom advisable to tell all. Be sparing, for instance, in the use of adverbs code comments…Let the conversationcode itself disclose the speaker’s manner or condition coder’s intention.

5.11. 不要解释过多

把话说尽是不可取的。比如尽量少用 副词 代码注释……让 对话 代码本身来显现 说话者的态度和状态 编码者的意图。

When we put on our “Bad Programmer” hats, on the other hand, we may not even be aware that the second audience, the human audience, exists. If we are aware it exists, we consider it secondary. In this state of temporary insanity, we might forget that, while part of our job is to talk to the compiler, the interpreter, and the toolchain, the bigger part of our job is to talk, through code, to our team members, maintainers, and stakeholders. Humans are the ultimate and primary audience for code. In that light, Strunk & White takes on new meaning and new relevance not just for coders, but for builders and creators of any stripe.

另一方面,当我们戴上“坏程序员”帽子时,我们甚至可能都不会意识到第二听众,即人类听众的存在。即使意识到了,我们也只是认为它是次要的。在这种短暂的失智中,我们也许忘了:尽管我们一部分工作是在与编译器、解释器和工具链交谈,但更多部分的是,通过代码,与我们团队成员、维护者和利益相关者们交谈。人类才是代码的终极听众。从这个角度来看,《风格的要素》不仅对写代码的人而且对各行各业的建造者和创造者都具有新的意义和联系。

翻译参考:
  • 斯特伦克, 怀特, & 陈一鸣. (1992). 英文写作指南. 上海: 上海译文出版社.

  • The Programming Aphorisms of Strunk & White | Coding the Wheel. (2015, February 23). Retrieved June 13, 2019, from https://web.archive.org/web/20150223232119/http://www.codingthewheel.com/archives/programming-aphorisms-of-strunk-and-white

扩展阅读:
  • Coding: It’s Just Writing. (n.d.). Retrieved July 1, 2019, from https://blog.codinghorror.com/coding-its-just-writing/


- 相关阅读 -
在技术洪流中看到我们的态度,第21期技术雷达正式发布!
标准化技术下的软件开发



点击【阅读原文】可至洞见网站查看原文&绿色字体部分的相关链接。

本文版权属ThoughtWorks公司所有,如需转载请在后台留言联系。

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存